#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <iostream>

#include "Object3D.h"
#include "Vertex3D.h"
#include "Triangle.h"
#include "Types.h"

using namespace std;

Object3D *obj;

Object3D *
read_orj (const char *path);

void render() 
{ 
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity(); 

    gluLookAt(0.0, 1.0, 6.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0);
	obj->draw();
    glutSwapBuffers();      
} 

void scale(int w, int h) 
{ 
  glViewport( 0, 0, w, h ); 
  
  glMatrixMode( GL_PROJECTION ); 
  glLoadIdentity(); 
  gluPerspective( 60., (double)w/(double)h, 1., 20. ); 
  
  glMatrixMode( GL_MODELVIEW ); 
}

void keyboard(unsigned char key, int x, int y) 
{
  switch (key) { 
  case 'q': 
    obj->set_state(EDITED);
	break;
  case 'w': 
    obj->set_state(NORMAL);
	break;
  case 'a':
    obj->set_state(SELECTED);
	break;
  }
  glutPostRedisplay();
}

int main( int argc, char** argv ) 
{ 
  GLfloat light_pos[] = {0.0, 4.0, 8.0, 0.0}; 
  GLfloat light[] = {1.0, 1.0, 1.0, 1.0};
  obj = read_orj("./mono.orj");
  glutInit( &argc, argv ); 
  
  glutInitDisplayMode( GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE ); 
  glutInitWindowSize(640, 480); 
  glutCreateWindow( "Select test" ); 
  
  glClearColor(0.2, 0.2, 0.2, 1.0);
  glShadeModel(GL_FLAT);
  glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, light);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_DEPTH_TEST);

  glutDisplayFunc(render); 
  glutReshapeFunc(scale); 
  glutKeyboardFunc(keyboard);
  
  glutMainLoop();  
  return 0; 
} 
